home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Visual Cafe 3
/
Visual Cafe 3.ISO
/
Vcafe
/
Sample.bin
/
EmployeeFileAccessor.java
< prev
next >
Wrap
Text File
|
1998-11-01
|
4KB
|
169 lines
import java.io.*;
/**
* Provide file storage and retrieval of employee data objects.
*/
public class EmployeeFileAccessor
{
RandomAccessFile f;
/**
* Open the employee file; create it if it does not exist.
*/
public EmployeeFileAccessor(String fname)
throws IOException
{
f = new RandomAccessFile(fname, "rw");
//{{INIT_CONTROLS
//}}
}
/**
* Set the employee file to the beginning
*/
public void reset()
throws IOException
{
f.seek(0);
}
// The size, in bytes, of an employee record. Records are fixed-size, to
// allow for easy modification.
final static int recsize =
Name.MAX_LENGTH+1 + Section.MAX_LENGTH+1 + SSN.MAX_LENGTH+1 +1;
/**
* Write a new employee to the end of the file
*/
public void addEmployee(Employee e)
throws IOException
{
// Transfer field values to buffer
byte[] b = new byte[recsize];
int pos = 0;
int len = e.name.value().length();
byte[] temp = e.name.value().getBytes();
inputBytes(temp, b, pos);
b[pos + len] = '\0';
pos += Name.MAX_LENGTH + 1;
String a = String.valueOf(e.section.value());
len = a.length();
temp = a.getBytes();
inputBytes(temp, b, pos);
b[pos + len] = '\0';
pos += Section.MAX_LENGTH + 1;
len = e.ssn.value().length();
temp = e.ssn.value().getBytes();
inputBytes(temp, b, pos);
b[pos + len] = '\0';
b[recsize-1] = '\n';
// Write buffer to file, as a fixed-size record
f.seek(f.length());
f.write(b);
}
public void inputBytes(byte[] from, byte[] to, int at) {
int len = from.length;
for (int i = 0; i < len; ++i) {
to[at + i] = from[i];
}
}
/**
* Fetch the first or next employee from the file.
*/
public Employee getNextEmployee()
throws IOException
{
byte[] b = new byte[recsize];
int nchars;
System.out.println("About to read");
if ((nchars = f.read(b)) <= 0) return null;
System.out.println("Read " + nchars + " chars");
int pos = 0;
int end = Name.MAX_LENGTH + 1;
String data = new String(b);
String n = "";
String a = "";
String s = "";
try {
n = data.substring(pos, end);
n = n.trim();
System.out.println("Fetched name='" + n + "'");
pos = end;
end += Section.MAX_LENGTH + 1;
a = data.substring(pos, end);
a = a.trim();
System.out.println("Fetched section=" + a);
pos = end;
end += SSN.MAX_LENGTH + 1;
s = data.substring(pos, end);
s = s.trim();
System.out.println("Fetched ssn=" + s);
}
catch (StringIndexOutOfBoundsException ex) {
System.out.println("Invalid employee read from file!");
return null;
}
Employee emp;
try
{
emp = new Employee(n, a, s);
}
catch (InvalidEmployeeException e)
{
System.out.println("Invalid employee read from file!");
return null;
}
return emp;
}
/**
* Read the employee file, and build a list of employee objects.
*/
EmployeeList buildEmployeeList()
{
EmployeeList elist = new EmployeeList();
Employee emp;
int noOfEmployees = 0;
try
{
reset();
}
catch (IOException ex)
{
return elist;
}
for (;;)
{
try
{
emp = getNextEmployee();
}
catch (IOException ex)
{
return elist;
}
if (emp == null) break;
noOfEmployees++;
elist.add(emp);
}
System.out.println("noOfEmployees=" + noOfEmployees);
return elist;
}
//{{DECLARE_CONTROLS
//}}
}